package com.yx.cloud.map.layer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

import com.aaa.lib.map.MapView;
import com.aaa.lib.map.layer.MapLayer;
import com.yx.cloud.R;
import com.iot.product.sweeper.bean.Map;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.common.utils.LogUtils;

import java.util.Arrays;

public class YXCustomMapLayer extends MapLayer {
    private static final int COLOR_BORDER = 0;
    private static final int COLOR_CLEANED = -1;
    private static final int COLOR_NONE = -2;
    MapInfo mapBean;
    Paint bgPaint;
    private int[] clipRect;
    /**
     *20种分区颜色
     */
/*    private int[] colors = new int[]{
            0xb8d2f1, 0xf9edb6, 0xc4bbf3, 0xf2c3b2,
            0x9cb4ef, 0xe9bf89, 0x92b5d3, 0xd1d9e9,
            0x88edd1, 0xf6e76d, 0xa9cef8, 0xf68c74,
            0xbfe590, 0xdeba54, 0x90ece7, 0xfd85aa,
            0x72cade, 0xfed353, 0x28e1bf, 0xffa647
    };*/
    //苗老师要改看效果的...
    private int[] colors = new int[]{
            0x88edd1, 0xecd653, 0xb7acef, 0xffc6b2, 0xa4ccfd, 0xfdc57e, 0xf4a7b9, 0xbfc6d3, 0xeda163, 0x92b5d3,
//            0xa9cef8, 0xf68c74, 0xbfe590, 0xdeba54, 0x90ece7, 0xfd85aa, 0x72cade, 0xfed353, 0x28e1bf, 0xffa647
            0x89d3f9, 0xf68c74, 0xa7d76b, 0xe6bc44, 0x62e6df, 0xfd85aa, 0x72cade, 0xf3c029, 0x32dab8, 0xffa647
    };
    private MapView map;
    private float[] matrixValue = new float[9];
    private Matrix matrix = new Matrix();
    private int scale = 4; //放大倍数, 为了保证图足够清晰 一定是整数

    public YXCustomMapLayer(MapView mapView) {
        super(mapView);
        map = mapView;
        bgPaint = new Paint();
        bgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        bgPaint.setStrokeWidth(1);
        bgPaint.setAntiAlias(true);
    }

    public YXCustomMapLayer(MapView mapView, Map map) {
        this(mapView);
        mapBean = map.getMap();
    }

    /**
     * 计算地图实际探测到的边界 用于居中
     */
    public static int[] getClipArea(int width, int height, int[] mapData) {
        int[] clipArea = new int[4];
        int minX = width;
        int minY = height;
        int maxX = 0;
        int maxY = 0;

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int type = mapData[i * width + j];
                if (type != COLOR_NONE) {
                    if (minX > j) {
                        minX = j;
                    }
                    if (minY > i) {
                        minY = i;
                    }
                    if (maxX < j) {
                        maxX = j;
                    }
                    if (maxY < i) {
                        maxY = i;
                    }
                }
            }
        }

        /**
         * 如果有数据 设置可裁剪的范围
         * 如果没有数据 设置为整个地图大小
         */
        if (minX <= maxX && minY <= maxY) {
            clipArea[0] = minX;
            clipArea[1] = minY;
            clipArea[2] = maxX;
            clipArea[3] = maxY;
        } else {
            clipArea[0] = 0;
            clipArea[1] = 0;
            clipArea[2] = width - 1;
            clipArea[3] = height - 1;
        }
        Log.i("getClipArea", Arrays.toString(clipArea));

        return clipArea;
    }

    public void setMapBean(MapInfo mapBean) {
        this.mapBean = mapBean;

        Bitmap old = getMapBitmap();
        if (old != null && !old.isRecycled()) {
            LogUtils.i("");
            old.recycle();
            setMapBitmap(null);
        }

        if (mapBean.mapData == null || mapBean.width == 0 || mapBean.height == 0) {
            setMapBitmap(null);
            return;
        }

        clipRect = getClipArea(mapBean.width, mapBean.height, mapBean.mapData);

        int in;
        int wall;
        int border;

        if (mapBean.mapType == mapBean.MAP_TYPE_GYRO) {
            //陀螺仪地图颜色：墙 #7ca5d6   地面 #a5ccfa
            in = mMapView.getContext().getColor(R.color.gyro_map_in);
            wall = mMapView.getContext().getColor(R.color.gyro_map_wall);
        } else {
            //激光地图墙的颜色：#677492  地面0x2F255C
            in = mMapView.getContext().getColor(R.color.map_in);
            wall = mMapView.getContext().getColor(R.color.map_wall);
        }
        border= Color.CYAN;

//        LogUtils.lm(mapBean.mapData,mapBean.width);
        int out = mMapView.getBackgroundColor();
        int[] mapData = new int[mapBean.mapData.length];
        for (int i = 0; i < mapBean.mapData.length; i++) {
            if (mapBean.mapData[i] == MapInfo.TYPE_WALL) {
                mapData[i] = wall;
            } else if (mapBean.mapData[i] == MapInfo.TYPE_ORIGIN) {
                mapData[i] = in;
            } else if (mapBean.mapData[i] == MapInfo.TYPE_NONE) {
                mapData[i] = out;
            }else if (mapBean.mapData[i] == MapInfo.TYPE_BORDER) {
                mapData[i] = border;
            } else {
                //各个分区不同颜色
                mapData[i] = colors[mapBean.mapData[i] % 20];
            }
        }

        setScale(mapBean.width, mapBean.height, 2048);

        int[] destData = fillBitmap(mapData, scale, mapBean.width, mapBean.height);
        Bitmap bitmap = Bitmap.createBitmap(destData, mapBean.width * scale, mapBean.height * scale, Bitmap.Config.RGB_565);
        //        Bitmap bitmap = Bitmap.createBitmap(mapData, mapBean.width, mapBean.height, Bitmap.Config.RGB_565);
        setMapBitmap(bitmap);
    }

    private void setScale(int mapWidth, int mapHeight, int targetSize) {
        float scaleWidth = (float) targetSize / (float) mapWidth;
        float scaleHeight = (float) targetSize / (float) mapHeight;
        scale = (int) (scaleWidth < scaleHeight ? scaleWidth : scaleHeight);
    }

    public int[] fillBitmap(int[] rawData, int scale, int width, int height) {
        int[] destData = new int[width * height * scale * scale];

        int scaleWidth = width * scale;
        LogUtils.i("dest size : " + destData.length + " rawData : " + rawData.length + " width : " + width + " height : " + height);
        for (int i = 0; i < destData.length; i++) {
            int rawX = (i % scaleWidth) / scale;
            int rawY = i / scaleWidth / scale;
            destData[i] = rawData[rawY * width + rawX];
        }
        return destData;
    }

    @Override
    public void draw(Canvas canvas) {
        if (!visible) {
            return;
        }

        if (getMapBitmap() != null) {
            setMatrix();
            canvas.drawBitmap(getMapBitmap(), matrix, bgPaint);
        }
    }

    public void setMatrix() {
        matrix.set(map.getTransform());
        matrix.preScale(1f / scale, 1f / scale);
    }

    public int[] getClipRect() {
        return clipRect;
    }

    public void setClipRect(int[] clipRect) {
        this.clipRect = clipRect;
    }
}
